//-----------------------------------------------------------------------
// <copyright file="tblOpicCodeDAOBase.cs" company="Scb">
//     Copyright ? Scb. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using Scb.Framework;
using Scb.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Robot.Entities;

namespace Robot.Data
{    
    public abstract class tblOpicCodeDAOBase
    {
        #region Common methods
        public virtual tblOpicCode CreatetblOpicCodeFromReader(IDataReader reader)
        {
            tblOpicCode item = new tblOpicCode();
            try
            {
                if (UtilityDAO.CheckColumnData(reader,"CustomerName")) item.CustomerName = (string)reader["CustomerName"];
                if (UtilityDAO.CheckColumnData(reader,"CustomerBranch")) item.CustomerBranch = (string)reader["CustomerBranch"];
                if (UtilityDAO.CheckColumnData(reader,"OpicsCode")) item.OpicsCode = (string)reader["OpicsCode"];
                if (UtilityDAO.CheckColumnData(reader,"Portfolio")) item.Portfolio = (string)reader["Portfolio"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreatetblOpicCodeFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreatetblOpicCode methods
            
        public virtual void CreatetblOpicCode(tblOpicCode tblOpicCode)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblOpicCodeInsert");
                
                database.AddInParameter(dbCommand, "@CustomerName", DbType.AnsiString, tblOpicCode.CustomerName);
                database.AddInParameter(dbCommand, "@CustomerBranch", DbType.AnsiString, tblOpicCode.CustomerBranch);
                database.AddInParameter(dbCommand, "@OpicsCode", DbType.AnsiString, tblOpicCode.OpicsCode);
                database.AddInParameter(dbCommand, "@Portfolio", DbType.AnsiString, tblOpicCode.Portfolio);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreatetblOpicCodeException, ex);
            }
        }

        #endregion

        #region UpdatetblOpicCode methods
        
        public virtual void UpdatetblOpicCode(tblOpicCode tblOpicCode)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblOpicCodeUpdate");            
                
                database.AddInParameter(dbCommand, "@CustomerName", DbType.AnsiString, tblOpicCode.CustomerName);
                database.AddInParameter(dbCommand, "@CustomerBranch", DbType.AnsiString, tblOpicCode.CustomerBranch);
                database.AddInParameter(dbCommand, "@OpicsCode", DbType.AnsiString, tblOpicCode.OpicsCode);
                database.AddInParameter(dbCommand, "@Portfolio", DbType.AnsiString, tblOpicCode.Portfolio);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdatetblOpicCodeException, ex);
            }
        }
        
        #endregion

        #region DeletetblOpicCode methods
        public virtual void DeletetblOpicCode(string customerName, string customerBranch)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblOpicCodeDelete");
                
                database.AddInParameter(dbCommand, "@CustomerName", DbType.AnsiString, customerName);
                database.AddInParameter(dbCommand, "@CustomerBranch", DbType.AnsiString, customerBranch);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeletetblOpicCodeException, ex);
            }
        }

        #endregion

        #region GettblOpicCode methods
        
        public virtual tblOpicCode GettblOpicCode(string customerName, string customerBranch)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblOpicCodeGet");
                
                database.AddInParameter(dbCommand, "@CustomerName", DbType.AnsiString, customerName);
                database.AddInParameter(dbCommand, "@CustomerBranch", DbType.AnsiString, customerBranch);
                
                tblOpicCode tblOpicCode =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        tblOpicCode = CreatetblOpicCodeFromReader(reader);
                        reader.Close();
                    }
                }
                return tblOpicCode;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGettblOpicCodeException, ex);
            }
        }
        
        #endregion

        #region GettblOpicCodeList methods
        public virtual tblOpicCodeCollection GettblOpicCodeList(tblOpicCodeColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblOpicCodeGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                tblOpicCodeCollection tblOpicCodeCollection = new tblOpicCodeCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        tblOpicCode tblOpicCode = CreatetblOpicCodeFromReader(reader);
                        tblOpicCodeCollection.Add(tblOpicCode);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return tblOpicCodeCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGettblOpicCodeListException, ex);
            }
        }
        
        public virtual tblOpicCodeCollection GettblOpicCodeList(tblOpicCodeColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GettblOpicCodeList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}